package com.classroompie.classroompiecommon.utils;


import com.classroompie.classroompiecommon.constant.ErrorConstant;
import com.classroompie.classroompiecommon.constant.ErrorConstant;
import com.classroompie.classroompiecommon.exception.FileException;
import com.classroompie.classroompiepojo.response.FileResponse;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class FileUtil {

    /*
     * rootFilePath表示路径前缀
     * 开发时可自定义，进入正式环境后需改为对应服务器地址
     * */
    public static final String rootFilePath = "D://test";


    /**
     * 删除文件
     */
    public static boolean delete(String[] paths) {
        for (String path : paths) {
            //根据路径创建文件对象
            File file = new File(rootFilePath + path);
            //合规性判断：路径是个文件且不为空
            if (file.isFile() && file.exists()) {
            } else {
                // 合规性不通过，抛出异常
                throw new FileException(ErrorConstant.FILE_PATH_FAILED + ": " + path);
            }

        }

        for (String path : paths) {
            File file = new File(rootFilePath + path);
            file.delete();
        }

        return true;
    }


    /**
     * 上传单个文件
     */
    public static FileResponse upload(MultipartFile file, String filePath, String fileId) {
        String fileType = Objects.requireNonNull(file.getOriginalFilename()).substring(file.getOriginalFilename().lastIndexOf(".") + 1);
        String newFileName = fileId + "." + fileType;
        String path = rootFilePath + filePath;

        //全路径存放在文件类中，判断文件夹是否存在不存在就创建
        File dataFile = new File(path);
        if (!dataFile.exists()) {
            dataFile.mkdirs();
        }

        FileResponse fileResponse = new FileResponse();

        //文件上传至指定路径
        try {
            file.transferTo(new File(dataFile, newFileName));
            // 封装响应结果
            fileResponse.setFileName(file.getOriginalFilename());
            fileResponse.setFileSize(file.getSize());
            fileResponse.setFileType(fileType);
            fileResponse.setFilePath(filePath + "\\" + newFileName);
            return fileResponse;
        } catch (IOException e) {
            throw new FileException(ErrorConstant.FILE_UPLOAD_FAILED);
        }
    }


    /**
     * 批量上传文件
     */
    public static List<FileResponse> batchUpload(MultipartFile[] files, String filePath) {
        List<FileResponse> fileResponses = new ArrayList<>();

        String path = rootFilePath + filePath;

        //全路径存放在文件类中，判断文件夹是否存在不存在就创建
        File dataFile = new File(path);
        if (!dataFile.exists()) {
            dataFile.mkdirs();
        }

        // 批量上传
        for (MultipartFile file : files) {
            FileResponse fileResponse = new FileResponse();
            String id = UUIDUtil.UUID();
            if (file.isEmpty()) {
                throw new FileException(ErrorConstant.PARAMS_ERROR);
            }
            String fileType = Objects.requireNonNull(file.getOriginalFilename()).substring(file.getOriginalFilename().lastIndexOf(".") + 1);
            String newFileName = id + "." + fileType;

            //文件上传至指定路径
            try {
                file.transferTo(new File(dataFile, newFileName));
            } catch (IOException e) {
                throw new FileException(ErrorConstant.FILE_UPLOAD_FAILED);
            }
            // 封装响应结果
            fileResponse.setFileName(file.getOriginalFilename());
            fileResponse.setFileSize(file.getSize());
            fileResponse.setFileType(fileType);
            fileResponse.setFilePath(path + "/" + newFileName);
            fileResponses.add(fileResponse);
        }

        return fileResponses;
    }

}
